<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/23
 * Time: 9:24
 */

namespace app\Admin\controller;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');
use think\facade\Env;

/**
 * @title 其他/杂项
 * @group Admin
 */
class Other extends Base
{
    /**
     * @title 图片上传接口
     * @author 开发者
     * @url /Admin/other/upload
     * @method POST
     * @param name:image require:1 type:string default:null desc:图片上传
     * @return data:图片的上传地址
     */
    public function upload(){
        //获取表单上传文件
        $file = request()->file('image');
        if($file){
            //获取应用根目录
            $root_path = Env::get('ROOT_PATH');
            // 移动到框架应用根目录/uploads/ 目录下 并进行验证
            $info = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif'])
                ->move($root_path.'public'.DIRECTORY_SEPARATOR.'uploads');
            if($info){
                $file_name =$file ->getFilename();
            }else{
                //返回上传错误信息
                $this->apiReturn(303,$file->getError());
            }
        }else{
            $this->apiReturn(303,'图片文件没有请求到！');
        }
        $this->apiReturn(200,'上传成功',''.'uploads/'.date('Ymd',time()).'/'.$file_name);
    }
}